<?php
namespace HelloAdmin\Controller;

class PublicController extends CommonController{


    protected $_needLogin = false;

    public function index() {
        if($this->_userId > 0){
            $this->redirect('admin/Index/index');exit;
        }
        $this->display();
    }

    public function test(){
       $path = './Public/location/content/2017/';
       $today = '20170622';
       $fh = opendir($path);
        while (($row = readdir($fh)) !== false) {
            if ($row == '.DS_Store' || $row == '..' || $row == '.') {
                continue;
            }
            echo '<br /><hr />'.$row.'<br/>';
            $newPaht = $path.$row.'/';
            $fh2 = opendir($newPaht);
            while (($filename = readdir($fh2)) !== false) {
                if ($filename == '.DS_Store' || $filename == '..' || $filename == '.') {
                    continue;
                }
                $pathFile = $newPaht.$filename;
                $time = substr($filename,0,8);
                if($time != $today){
                    unlink($pathFile);
                    echo $filename.'已删除 <br>';
                }
            }


        }
    }

    //上传网络图片
    public function networkImage(){
        $url = I('post.url');
        if(empty($url)){
            $this->display('networkImage');
        }else{
            if(imgUlrExists($url)){
                echo $url;
            }
        }
    }


    public function getVerify() {
        $config = array(
            'fontSize' => 20, // 验证码字体大小
            'length' => 10, // 验证码位数
            'width' => 300,
            'height' => 30,
            'useNoise' => TRUE, // 关闭验证码杂点
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    public function checkLogin() {
        $verify = I('post.verify');
        if($verify != 'tong896'){
            $this->error('验证码错误');exit;
        }
        $username = I('post.username');
        $password = I('post.password');
        $User = new \HelloAdmin\Model\UserModel();
        $info = $User->checkRepeat($username);
        if($info){
            if($info['password'] === genSecretStr($password,$info['pass_account'])){
                session(C('USER_AUTH_KEY'),$info);
                session('user_id', $info['user_id']);
                $data = array('last_time'=>time());
                $User->where("`{$User->getPk()}` = '{$info['user_id']}'")->save($data);
                $this->redirect('Index/index');exit;
            }
        }
        $this->error('用户名或密码错误','Public/index');exit;
    }

    public function checkVerify($code) {
        $verify = new \Think\Verify();
        return $verify->check($code);
    }


    public function UploadImg(){
        $path = I('request.savePath');
        switch ($path) {
            case 'img':$savePath=\Common\Library\COM\CollectDo::doImagePath();break;
            case 'content':$savePath= \Common\Library\COM\CollectDo::doContentPath();break;
            default:
                $savePath = '';
                break;
        }
        if(!empty($savePath)){
            $info = pathinfo($savePath);
            $savePath = $info['dirname'].'/';
            $saveName = $info['filename'];
        }//p($info);p($savePath);exit;
        parent::UploadImg(array(
                'maxSize'=>5000000,
                'savePath'=>$savePath,
                'saveName'=>$saveName
            ));exit;
    }

    public function crontab(){
        $Crontab = new \HelloAdmin\Model\CrontabModel();
        $cron = $Crontab->where("`type`='add' AND `disabled`='1'")->order('`id` ASC')->find();
        if($cron['id'] > 0){
            $data = array('dotime'=>time(),'disabled'=>2,'dotime'=>time(),'remark'=>'任务计划开始执行');
            $Crontab->where("`id`='{$cron['id']}'")->save($data);
            $int = strpos($cron['url'],'www.37zw');
            unset($Crontab,$data);
            if($int > 0){
                new \Common\Library\biquge($cron['id'],$cron['url']);
            }
            $int5 = strpos($cron['url'],'xs.la');
            $int50 = strpos($cron['url'],'biquge.tw');
            if($int5 > 0 || $int50 > 0){
                new \Common\Library\biqugela($cron['id'],$cron['url']);
            }
            $int6 = strpos($cron['url'],'www.51shucheng');
            if($int6 > 0){
                new \Common\Library\wuyishucheng($cron['id'],$cron['url']);
            }
            $int7 = strpos($cron['url'],'www.baishuku');
            if($int7 > 0){
                new \Common\Library\baishuku($cron['id'],$cron['url']);
            }
            $int8 = strpos($cron['url'],'www.bokbao');
            if($int8 > 0){
                new \Common\Library\bokbao($cron['id'],$cron['url']);
            }
        }else{
            $r = mt_rand(1,4);
            switch ($r) {
                case 1:
                    new \Common\Library\biquge(0,'');
                    break;
                case 2:
                    new \Common\Library\biquge(0,'');
                    break;
                // case 3:
                //     new \Common\Library\baishuku(0,'');
                //     break;
                case 3:
                    new \Common\Library\bokbao(0,'');
                    break;
                case 4:
                    new \Common\Library\biqugela(0,'');
                    break;
            }
        }
    }

    public function crontabTest(){

        //new \Common\Library\yuyouge(0,'http://www.yuyouge.com/xs/557/index.html');
        //new \Common\Library\baishuku(0,'http://baishuku.com/booksinfoinfo/127/127414.htm');
        //new \Common\Library\x23us(0);
        //new \Common\Library\biqugela(0,'http://www.xs.la/64_64749/');
        //new \Common\Library\wuyishucheng(0,'http://www.51shucheng.net/zuojia/hanhan','authorlist');
        //new \Common\Library\wuyishucheng(0,'http://www.51shucheng.net/xiandai','booklist');
    }

    public function soTop(){
        set_time_limit(0);
        $cates = array('玄幻奇幻','都市言情','穿越重生','都市异能','乡村生活','官场职场');
        $pathFile = './Runtime/Collect/37zw/getBooks.html';
        \Common\Library\COM\CollectDo::downPage('http://top.so.com/index.php?page=2&tag=小说%7C青春校园&c=小说&m=Index',$pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile,'','utf-8');
        $bookInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'搜索指数</th>','<div class="footer">');
        p($getStr);exit;
        $Setting = D('Common/Setting');
        foreach ($urlList as $val) {
            # code...
            \Common\Library\COM\CollectDo::downPage($val,$pathFile);
            $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
            $bookInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'搜索指数</th>','<div class="footer">');
            unset($getStr);
            $count = mb_substr_count($bookInfo,'<td class="keyword">');
            $books = '';
            for($i=0;$i<$count;$i++){
                $now =  \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<a class="list-title"','_blank">search</a>');
                $bookName = \Common\Library\COM\CollectDo::getSubstr($now,'">','</a>');
                $books .= $bookName.':';
                $bookInfo = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<td class="tc">','',false);
            }
            $old = D('Common/Setting')->getValue('baiduTop');
            if(!empty($old)){
                $value = $old.':'.trim($books,':');
            }else{
                $value = trim($books,':');
            }
            $data = array('name'=>'baiduTop','value'=>$value);
            $Setting->saveValue($data);
        }
        $data = explode(':',$Setting->getValue('baiduTop'));
        $data = array_unique($data);
        $data = implode($data, ':');
        $data = array('name'=>'baiduTop','value'=>$data);
        $Setting->saveValue($data);
        exit('over good');
    }

    public function baiduTop(){
        set_time_limit(0);
        $urlList = array(
                'http://top.baidu.com/buzz?b=7&c=10&fr=topcategory_c10',
                'http://top.baidu.com/buzz?b=353&c=10&fr=topbuzz_b355_c10',
                'http://top.baidu.com/buzz?b=355&c=10&fr=topbuzz_b353_c10',
                'http://top.baidu.com/buzz?b=354&c=10&fr=topbuzz_b355_c10',
                'http://top.baidu.com/buzz?b=1508&c=10&fr=topbuzz_b354_c10',
                'http://top.baidu.com/buzz?b=1509&c=10&fr=topbuzz_b1508_c10',
                'http://top.baidu.com/buzz?b=356&c=10&fr=topbuzz_b1509_c10',
                'http://top.baidu.com/buzz?b=459&c=10&fr=topbuzz_b356_c10',
                'http://top.baidu.com/buzz?b=1512&c=10&fr=topbuzz_b459_c10',
                'http://top.baidu.com/buzz?b=1510&c=10&fr=topbuzz_b1512_c10',
                'http://top.baidu.com/buzz?b=1513&c=10&fr=topbuzz_b1510_c10'
            );
        $pathFile = './Runtime/Collect/37zw/baiduTop.html';
        $Setting = D('Common/Setting');
        //p(explode(':',$Setting->getValue('baiduTop')));exit;

        $data = array('name'=>'baiduTop','value'=>'');
        $Setting->saveValue($data);
        foreach ($urlList as $val) {
            # code...
            \Common\Library\COM\CollectDo::downPage($val,$pathFile);
            $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
            $bookInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'搜索指数</th>','<div class="footer">');
            unset($getStr);
            $count = mb_substr_count($bookInfo,'<td class="keyword">');
            $books = '';
            for($i=0;$i<$count;$i++){
                $now =  \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<a class="list-title"','_blank">search</a>');
                $bookName = \Common\Library\COM\CollectDo::getSubstr($now,'">','</a>');
                $books .= $bookName.':';
                $bookInfo = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<td class="tc">','',false);
            }
            $old = $Setting->getValue('baiduTop');
            if(!empty($old)){
                $value = $old.':'.trim($books,':');
            }else{
                $value = trim($books,':');
            }
            $data = array('name'=>'baiduTop','value'=>$value);
            $Setting->saveValue($data);
        }
        $data = explode(':',$Setting->getValue('baiduTop'));
        $data = array_unique($data);
        $data = implode($data, ':');
        $data = array('name'=>'baiduTop','value'=>$data);
        $Setting->saveValue($data);
        p($Setting->getValue('baiduTop'));
        exit('over good');

    }



}